package com.justRun;

import com.justRun.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class HomeActivity extends Activity {
	private String fileName = "prefs.txt";
	private Context lecontext = this;
	private String poids= "Poids";
	private String age = "Age";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		initButtonsHome();
		ReadSettings(lecontext);
	}

	private void initButtonsHome() {
		((Button) findViewById(R.id.runButtonHome)).setOnClickListener(run);
		((Button) findViewById(R.id.pathButtonHome)).setOnClickListener(path);
		((Button) findViewById(R.id.statsButtonHome)).setOnClickListener(stats);
		((Button) findViewById(R.id.buttonPushMe)).setOnClickListener(run);
		
		((EditText) findViewById(R.id.editTextAge)).setOnClickListener(setAge);
		((EditText) findViewById(R.id.editTextPoids)).setOnClickListener(setPoids);
	}
	
	private OnClickListener path = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			saveAgePoids();
			if (poids!="Poids" && poids.length()>1) {
				WriteSettings(lecontext, poids + "\n" + age);
				Intent myIntent = new Intent(arg0.getContext(),
						PathActivity.class);
				startActivity(myIntent);
				finish();
			}else{
				Toast.makeText(lecontext, "Veuillez indiquer votre poids",Toast.LENGTH_SHORT).show(); 
			}
		}
	};

	private void saveAgePoids(){
		poids = ((EditText) findViewById(R.id.editTextPoids)).getText().toString();
		age = ((EditText) findViewById(R.id.editTextAge)).getText().toString();
	}

	private OnClickListener setAge = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			((EditText) findViewById(R.id.editTextAge)).setText("");
		}
	};

	private OnClickListener setPoids = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			((EditText) findViewById(R.id.editTextPoids)).setText("");
		}
	};

	private OnClickListener stats = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			saveAgePoids();
			if (poids!="Poids" && poids.length()>1) {

				WriteSettings(lecontext, poids + "\n" + age);
				Intent myIntent = new Intent(arg0.getContext(),
						StatsActivity.class);
				startActivity(myIntent);
				finish();
			}else{
				Toast.makeText(lecontext, "Veuillez indiquer votre poids",Toast.LENGTH_SHORT).show(); 
			}
		}
	};

	private OnClickListener run = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			saveAgePoids();

			if ((!poids.equals("Poids")) && poids.length()>1) {
				WriteSettings(lecontext, poids + "\n" + age);
				Intent myIntent = new Intent(arg0.getContext(),
						RunActivity.class);
				startActivity(myIntent);
				finish();
			}else{
				Toast.makeText(lecontext, "Veuillez indiquer votre poids",Toast.LENGTH_SHORT).show(); 
			}
		}
	};

	public void ReadSettings(Context context){
		FileManager fm = new FileManager(this, fileName);
		try {
			fm.startReading();
			poids = fm.readLine();
			age = fm.readLine();
			fm.stopReading();
		} catch (Exception e) {
			e.printStackTrace();
		}
		((EditText) findViewById(R.id.editTextAge)).setText(age);
		((EditText) findViewById(R.id.editTextPoids)).setText(poids);
	}

	public void WriteSettings(Context context, String data){ 
		FileManager fm = new FileManager(this, fileName);
		fm.deleteFile();
		fm.startWriting();
		fm.writeLine(data);
		fm.stopWriting();

	}
}
